﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeWallDestroyed : MonoBehaviour {

    private float _effectDistance = 0.2f;

	public void DoBeforeDestroy(Transform collider)
    {
        Ray rayLeft = new Ray(transform.position, collider.TransformDirection(Vector3.left));
        Ray rayRight = new Ray(transform.position, collider.TransformDirection(Vector3.right));

        Debug.DrawRay(rayLeft.origin, rayLeft.direction * _effectDistance, Color.red, 2);
        Debug.DrawRay(rayRight.origin, rayRight.direction * _effectDistance, Color.green, 2);

        RaycastHit hit;
        if(Physics.Raycast(rayLeft, out hit, _effectDistance)) 
        {
            CubeWallDestroyed boom = hit.collider.GetComponent<CubeWallDestroyed>();
            if (boom != null) boom.FollowDestroy();
        }

        if(Physics.Raycast(rayRight, out hit, _effectDistance))
        {
            CubeWallDestroyed boom = hit.collider.GetComponent<CubeWallDestroyed>();
            if (boom != null) boom.FollowDestroy();
        }

    }

    public void FollowDestroy()
    {
        Destroy(gameObject);
    }
}
